package com.neusoft.chanceserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.neusoft.chanceserver.dto.RespBean;
import com.neusoft.chanceserver.dto.TrackAdd;
import com.neusoft.chanceserver.entity.ChanceTrack;
import com.neusoft.chanceserver.entity.Track;
import com.neusoft.chanceserver.mapper.ChanceTrackMapper;
import com.neusoft.chanceserver.mapper.TrackMapper;
import com.neusoft.chanceserver.service.ITrackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 机会跟踪记录表 服务实现类
 * </p>
 *
 * @author liuxp
 * @since 2021-07-12
 */
@Service
public class TrackServiceImpl extends ServiceImpl<TrackMapper, Track> implements ITrackService {

    @Autowired
    TrackMapper trackMapper;

    @Autowired
    ChanceTrackMapper chanceTrackMapper;

    @Override
    public RespBean deleteTrack(Integer trackId) {
        RespBean respBean = RespBean.ok(200, "Track deleted successfully!");
        trackMapper.deleteById(trackId);
        LambdaQueryWrapper<ChanceTrack> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ChanceTrack::getTrackId, trackId);
        chanceTrackMapper.delete(wrapper);
        return respBean;
    }

    @Override
    public RespBean addTrack(TrackAdd trackAdd) {
        RespBean respBean = RespBean.ok(200, "Track added successfully!");
        trackMapper.insert(trackAdd.getTrack());
        int tractId = trackAdd.getTrack().getTrackId();
        ChanceTrack chanceTrack = new ChanceTrack();
        chanceTrack.setChanceId(trackAdd.getChanceId());
        chanceTrack.setTrackId(tractId);
        chanceTrackMapper.insert(chanceTrack);
        return respBean;
    }

    @Override
    public RespBean updateTrack(Track track) {
        RespBean respBean = RespBean.ok(200, "Track updated successfully!");
        trackMapper.updateById(track);
        return respBean;
    }

    @Override
    public RespBean searchTrackByChanceId(Integer chanceId) {
        List<Integer> trackIds = new ArrayList<>();
        LambdaQueryWrapper<ChanceTrack> chanceTrackLambdaQueryWrapper = Wrappers.lambdaQuery();
        chanceTrackLambdaQueryWrapper.eq(ChanceTrack::getChanceId, chanceId);
        List<ChanceTrack> chanceTracks = chanceTrackMapper.selectList(chanceTrackLambdaQueryWrapper);
        for(ChanceTrack chanceTrack : chanceTracks){
            trackIds.add(chanceTrack.getTrackId());
        }
        List<Track> tracks = new ArrayList<>();
        if(trackIds.size() > 0)
            tracks = trackMapper.selectBatchIds(trackIds);
        RespBean respBean = RespBean.ok(200, "Find " + tracks.size() + " tracks!");
        respBean.setObj(tracks);
        return respBean;
    }

}
